import {
  defineStore
} from "pinia";
import {
  getinfo,
  logout
} from "@/api/user";
import {
  removeToken
} from "@/utils/auth";
export const user = defineStore({
  id: "user",
  state: () => {
    return {
      userInfo: {},
      token: "",
      menus: [],
      ruleNames: [],
      // 侧边宽度
      asideWidth: "250px",
    };
  },
  getters: {},
  actions: {
    SET_WIDTH(){
      this.asideWidth = this.asideWidth==='250px'?'64px':'250px'
    },
    setToken(val) {
      this.token = val;
    },
    getInfo() {
      return new Promise((resolve, reject) => {
        getinfo().then(
          (res) => {
            this.userInfo = res.data;
            this.menus = res.data.menus;
            this.ruleNames = res.data.ruleNames
            return resolve(res);
          },
          (error) => {
            return reject(error);
          }
        );
      });
    },
    logout() {
      return new Promise((resolve, reject) => {
        logout().then(
          (res) => {
            if (res.status === 200) {
              this.userInfo = {};
              this.menus = [];
              removeToken();
              resolve(res);
            }
          },
          (error) => {
            reject(error);
          }
        );
      });
    },
  },
});